package com.lovo.product.service.impl;

import com.lovo.common.bean.Product;
import com.lovo.product.esp.ProductElasticsearchDAO;
import com.lovo.product.service.ProductElasticsearchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.StreamSupport;

@Service
public class ProductElasticsearchServiceImpl implements ProductElasticsearchService {
    @Autowired
    private ProductElasticsearchDAO productElasticsearchDAO;
    @Override
    public List findAll() {
        Iterable<Product> iterable = productElasticsearchDAO.findAll();
        return StreamSupport.stream(iterable.spliterator(),true)
                .collect(Collectors.toList());
    }

    @Override
    public Page<Product> findAllByPage(Integer current, Integer pageSize) {
        PageRequest pageRequest = PageRequest.of(current - 1,pageSize);
        return productElasticsearchDAO.findAll(pageRequest);
    }

    @Override
    public List findByName(String name) {
        return productElasticsearchDAO.findByName(name);
    }

    @Override
    public void add(Product product) {
        productElasticsearchDAO.save(product);
    }

    @Override
    public void update(Product product) {
        productElasticsearchDAO.save(product);
    }

    @Override
    public void delete(Integer id) {
        productElasticsearchDAO.deleteById(id);
    }
}
